﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace eTraining
{
    public partial class frmBase : Form
    {
        public frmBase()
        {
            InitializeComponent();
        }

        private void frmBase_Resize(object sender, EventArgs e)
        {
            StyleHelper.SetWindowsRegion(this);
        }

        private Bitmap buffer = new Bitmap(1, 1);

        protected override void OnPaintBackground(PaintEventArgs e)
        {
            if (buffer.Size.Equals(this.Size) == false)
            {
                buffer = new Bitmap(Math.Max(1, Width), Math.Max(1, Height));
            }

            Graphics g = Graphics.FromImage(buffer);

            PaintEventArgs pe = new PaintEventArgs(g, e.ClipRectangle);

            base.OnPaintBackground(pe);

            Draw(g);

            e.Graphics.DrawImage(buffer, 0, 0);

            g.Dispose();
        }

        protected virtual void Draw(Graphics g)
        {
            Rectangle rect = new Rectangle(
                Padding.Left,
                Padding.Top,
                Width- Padding.Horizontal,
                Height - Padding.Vertical
            );

            SolidBrush sBrush = new SolidBrush(Color.WhiteSmoke);
            g.FillRectangle(sBrush, rect);
            sBrush.Dispose();

            g.DrawRectangle(Pens.DimGray, rect);
            g.DrawLine(Pens.DimGray, rect.Left, rect.Top + 20, rect.Right, rect.Top + 20);
            g.DrawLine(Pens.Silver, rect.Left, rect.Top + 21, rect.Right, rect.Top + 21);

            DrawText(g);
        }

        protected virtual void DrawText(Graphics g )
        {
            SizeF s = g.MeasureString(Text, Font);

            SolidBrush sb = new SolidBrush(ForeColor);

            g.DrawString(
                Text,
                Font,
                sb,
                Padding.Left,
                Padding.Top + 20  - s.Height
            );

            sb.Dispose();

        }
    }
}
